<?php

namespace app\api\model;

use think\Model;

class Theme extends Base
{
    protected $table = 'theme';
    protected $autoWriteTimestamp = 'datetime';

    public function topicImg()
    {
        return $this->belongsTo('Image', 'topic_img_id', 'id');
    }

    public function headImg()
    {
        return $this->belongsTo('Image', 'head_img_id', 'id');
    }

    public function products()
    {
        return $this->belongsToMany('Product', 'theme_product', 'product_id', 'theme_id');
    }


    public function getThemes(){

        try{
            $theme = $this->with(['topicImg'])->select();

        }catch (\Exception $e){

            $theme = null;

        }
        return $theme;
    }

    public function getThemeWithProducts($id)
    {
        $themes = $this->with('products,topicImg,headImg')
            ->find($id);
        return $themes;
    }
}